| Conditions | 1 |
| Paths | 1 |
| Total Lines | 61 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /** |
||
| 16 | function elements($timeout, state, data) { |
||
| 17 | let ct = this; |
||
| 18 | ct.elementPrice = 1; |
||
| 19 | ct.state = state; |
||
| 20 | ct.data = data; |
||
| 21 | ct.outcome = {}; |
||
| 22 | |||
| 23 | ct.getChance = function(element) { |
||
| 24 | let bonus = 0; |
||
| 25 | for(let isotope in data.elements[element].isotopes){ |
||
| 26 | bonus += state.player.resources[isotope].number*data.constants.ELEMENT_CHANCE_BONUS; |
||
| 27 | } |
||
| 28 | return Math.min(1, data.elements[element].abundance*(1+bonus)); |
||
| 29 | }; |
||
| 30 | |||
| 31 | ct.buyElement = function (element) { |
||
| 32 | if (state.player.elements[element].unlocked) { |
||
| 33 | return; |
||
| 34 | } |
||
| 35 | if (state.player.resources.dark_matter.number >= ct.elementPrice) { |
||
| 36 | state.player.resources.dark_matter.number -= ct.elementPrice; |
||
| 37 | |||
| 38 | if(Math.random() < ct.getChance(element)){ |
||
| 39 | state.player.elements[element].unlocked = true; |
||
| 40 | state.player.elements[element].generators['1'] = 1; |
||
| 41 | state.player.elements_unlocked++; |
||
| 42 | ct.outcome[element] = 'Success'; |
||
| 43 | }else{ |
||
| 44 | ct.outcome[element] = 'Fail'; |
||
| 45 | } |
||
| 46 | $timeout(function(){ct.clearMessage(element);}, 1000) |
||
| 47 | } |
||
| 48 | }; |
||
| 49 | |||
| 50 | ct.clearMessage = function (element) { |
||
| 51 | ct.outcome[element] = ''; |
||
| 52 | }; |
||
| 53 | |||
| 54 | /* This function returns the class that determines on which |
||
| 55 | colour an element card */ |
||
| 56 | ct.elementClass = function (element) { |
||
| 57 | if(!state.player.elements[element]){ |
||
| 58 | return 'element_unavailable'; |
||
| 59 | } |
||
| 60 | if (state.player.elements[element].unlocked) { |
||
| 61 | return 'element_purchased'; |
||
| 62 | }else{ |
||
| 63 | if(state.player.resources.dark_matter.number >= ct.elementPrice) { |
||
| 64 | return 'element_cost_met'; |
||
| 65 | }else{ |
||
| 66 | return 'element_cost_not_met'; |
||
| 67 | } |
||
| 68 | } |
||
| 69 | }; |
||
| 70 | |||
| 71 | /* This function returns the class that determines the secondary |
||
| 72 | colour of an element card */ |
||
| 73 | ct.elementSecondaryClass = function (element) { |
||
| 74 | return ct.elementClass(element) + '_dark'; |
||
| 75 | }; |
||
| 76 | } |
||
| 77 |